<%@page import="java.sql.ResultSet"%>
<%@page import="Modelo.Querys"%>
<%@page import="Controlador.ConnectionDB"%>
<%@page pageEncoding="UTF-8" %>
<% 
String TypeMessage = "info";
String FinalMessage = "Incidencia asignada correctamente";
String Accion = request.getParameter("Type");
if(Accion.equals("1")){
    ConnectionDB c = new ConnectionDB();
    String Vulnerability = request.getParameter("Vulnerability");
    if(c.Connect()){
        ResultSet rs = c.getQuery("select b.code ,b.name, a.percentage from vulnerabilityincident a, incident b where a.incident = b.code and a.vulnerability = "+Vulnerability);
        if(c.getNumRows(rs)> 0){
%>
<section class="tablecss" >
    <table>
        <thead>
            <tr>
                <th>Incidente:</th>
                <th>Porcentaje:</th>
                <th>Acción:</th>
            </tr>
        </thead>
        <tbody>
            <% 
            while(rs.next()){
            %>
            <tr class="claro">
                <td><%=rs.getString(2)%> </td>
                <td><%=rs.getString(3)%> </td>
                <td><img src="images/delete.png" onclick="javascript:deleteincident('<%=rs.getString(1)%>')"/></td>
            </tr>
            <%
            
            if(rs.next()){
            %>
            <tr class="oscuro">
                <td><%=rs.getString(2)%> </td>
                <td><%=rs.getString(3)%> </td>
                <td><img src="images/delete.png" onclick="javascript:deleteincident('<%=rs.getString(1)%>')"/></td>
            </tr>
            <%
                       }
            }
        %>
        </tbody>
    </table>
    </br>
</section>

<%
        }else{
            out.print("<p>No se encontraron Incidentes asignados a esta Vulnerabilidad</p>");
        }
c.close();
    }
   
}else if(Accion.equals("2")){
    String Incident = request.getParameter("Incident");
    String Vulnerability = request.getParameter("Vulnerability");
    ConnectionDB c = new ConnectionDB();
    if(c.Connect()){
        c.getUpdate("delete from vulnerabilityincident where incident = '"+ Incident+"' and vulnerability = "+Vulnerability);
        c.commit(true);
        c.close();
    }
} else if (Accion.equals("3")){
    String Vulnerability = request.getParameter("SelectVulnerability");
    String Incident = request.getParameter("SelectIncident");
    String Percentage = request.getParameter("Percentage");
    int p=0;
    ConnectionDB c = new ConnectionDB();
    if(c.Connect()){
        ResultSet rs = c.getQuery("select sum(percentage) from vulnerabilityincident where vulnerability = "+Vulnerability+" group by vulnerability;");
        if(c.getNumRows(rs)>0){
        rs.next();        
        p = Integer.parseInt(rs.getString(1));
        }
        if(p > 100){
            FinalMessage = "No se pueden agregar mas incidencias, ya supera el 100%";
            TypeMessage = "warning";
        }else if ((p = p + Integer.parseInt(Percentage)) > 100){
            FinalMessage = "No se pueden agregar la incidencia con porcentaje "+Percentage+"%, ya que supera el 100%";
            TypeMessage = "warning";
        }else{
            c.getUpdate("insert into vulnerabilityincident (vulnerability, incident, percentage) values("+Vulnerability+",'"+Incident+"', "+Percentage+");");
            c.commit(true);
        }
        c.close();
    }else{
        FinalMessage = "No se pudo Conectar a la base de datos.";
        TypeMessage = "error";
    }
%>
<jsp:forward page="asig_vul_inc.jsp">
    <jsp:param name="FinalMessage" value="<%=FinalMessage%>" />       
    <jsp:param name="TypeMessage" value="<%=TypeMessage%>"/>
</jsp:forward>
<%
}
%>